package leetcode.slidwidow;

public class Lc3095_2 {


    public int minimumSubarrayLength(int[] nums, int k) {
        int ans = Integer.MAX_VALUE;
        int l = 0, r = 0;
        int[] bits = new int[30];
        for (; r < nums.length; r++) {
            for (int i = 0; i < 30; i++) {
                bits[i] += ((nums[r] >> i) & 1);
            }
            while (l <= r && cacul(bits) >= k) {
                ans = Math.min(ans, r - l + 1);
                for (int i = 0; i < 30; i++) {
                    bits[i] -= (nums[l] >> i) & 1;
                }
                l++;
            }
        }
        return ans == Integer.MAX_VALUE ? -1 : ans;
    }

    private int cacul(int[] bits) {
        int ans = 0;
        for (int i = 0; i < bits.length; i++) {
            if (bits[i] > 0)
                ans |= (1 << i);
        }
        return ans;
    }



    public static void main(String[] args) {
        Lc3095_2 lc3095_2 = new Lc3095_2();
        int[] a = {1,2,8};
        int k = 10;
        System.out.println(lc3095_2.minimumSubarrayLength(a, k));
    }


}
